package com.fyt.service.Impl.user.account;

import com.fyt.entity.User;
import com.fyt.service.Impl.utils.UserDetailsImpl;
import com.fyt.service.user.account.LoginOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
public class LoginOutServiceImpl implements LoginOutService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Boolean loginOut() {
        //从Security上下文获取存储的user信息
        UsernamePasswordAuthenticationToken authentication =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        User user = loginUser.getUser();
        String userId = user.getId()+"";
        Boolean delete = redisTemplate.delete(userId);//清除redis中的token
        SecurityContextHolder.clearContext(); //清除Security中的权限用户等信息
        return delete;
    }
}
